﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditRoom : AdminPageBase
    {

        protected List<UtilityInfo> UtilityList = new List<UtilityInfo>(); 
       
        private RoomInfo _roomInfo = new RoomInfo();
        private readonly RoomController _roomController = new RoomController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["RoomId"]))
                _roomInfo =new RoomController().GetRoom(int.Parse(Request.QueryString["RoomId"]));

            if (Page.IsPostBack) return;

            LoadRoomType();
            LoadStatus();
            LoadUser();
            LoadHotel();

            UtilityList = new UtilityController().ListUtility().Where(p=>p.UtilityType==2).OrderBy(p => p.UtilityId).ToList();

            if (_roomInfo.RoomId == -1) return;
            

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListRoom.aspx?xml=Room");
        }

        private void SaveData()
        {
            _roomInfo.RoomName= txtRoomName.Text;
            _roomInfo.HotelId = int.Parse(ddlHotel.SelectedValue);
            _roomInfo.RoomTypeId = int.Parse(ddlRoomType.SelectedValue);

            _roomInfo.SortDescription = txtSortDesription.Text;
            _roomInfo.DetailDescription = txtContent.Text;
            _roomInfo.Utilities = txtUtilities.Value;

            _roomInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _roomInfo.RoomQuantity = int.Parse((txtRoomQuantity.Text));
            _roomInfo.PersonQuantity= int.Parse((txtPersonQuantity.Text));
            _roomInfo.Acreage = int.Parse((txtAcreage.Text));



            if (_roomInfo.RoomId== -1){
                _roomInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _roomInfo.CreatedDate = DateTime.Now;
                _roomController.InsertRoom(_roomInfo);
            }
            else
            {
                //_roomInfo.UpdatedBy = int.Parse(ddlCreatedBy.SelectedValue);
                _roomInfo.UpdatedDate = DateTime.Now;
                _roomController.UpdateRoom(_roomInfo);
            }

            Response.Redirect("/Pages/ListRoom.aspx?xml=Room");
        }

        private void RenderForm()
        {
            txtRoomName.Text = _roomInfo.RoomName;
            ddlHotel.SelectedValue = _roomInfo.HotelId.ToString();
            ddlRoomType.SelectedValue = _roomInfo.RoomTypeId.ToString();

            txtSortDesription.Text = _roomInfo.SortDescription;
            txtContent.Text = _roomInfo.DetailDescription;
            txtUtilities.Value = _roomInfo.Utilities;   
            ddlStatus.SelectedValue = _roomInfo.Status.ToString();
            txtRoomQuantity.Text = _roomInfo.RoomQuantity.ToString();
            txtPersonQuantity.Text = _roomInfo.PersonQuantity.ToString();
            txtAcreage.Text = _roomInfo.Acreage.ToString();
            ddlCreatedBy.SelectedValue = _roomInfo.CreatedBy.ToString();
        }

        private void LoadRoomType()
        {
            ddlRoomType.DataSource =
               new RoomTypeController().ListRoomType()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.RoomTypeName);
            ddlRoomType.DataTextField = "RoomTypeName";
            ddlRoomType.DataValueField = "RoomTypeId";
            ddlRoomType.DataBind();

            ddlRoomType.Items.Insert(0, new ListItem("--- Loại phòng ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadHotel()
        {
            ddlHotel.DataSource =
               new HotelController().ListHotel()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.HotelFullName);
            ddlHotel.DataTextField = "HotelFullName";
            ddlHotel.DataValueField = "HotelId";
            ddlHotel.DataBind();

            ddlHotel.Items.Insert(0, new ListItem("--- Chọn khách sạn ---", "-1"));
        }
    }
}